
package org.beaconframework.ext.google.code;

import java.io.InputStream;
import java.util.Map;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.xml.parsers.DocumentBuilderFactory;

import org.beaconframework.ext.rs.RestQueryTemplate;
import org.beaconframework.ext.rs.RestXmlItemContainer;
import org.beaconframework.ext.rs.WebResource;
import org.beaconframework.ext.rs.WebResourceFactory;
import org.beaconframework.util.xml.NamespaceContextRegistry;
import org.beaconframework.vaadin.util.VaadinContext;
import org.w3c.dom.Document;

import com.vaadin.ui.Table;

public class ComponentBuilder {

    public Table createIssueTable() {

        Table t = new com.vaadin.ui.Table() {

            @Override
            public void attach() {
                // TODO Auto-generated method stub
                super.attach();
                ((RestXmlItemContainer) getContainerDataSource()).refresh();
            }

        };
        RestXmlItemContainer x = new RestXmlItemContainer();
        NamespaceContextRegistry ncr = new NamespaceContextRegistry();

        x.getResolverRegistry().getNamespaceContextRegistry().addNamespace(
                "atom", "http://www.w3.org/2005/Atom");
        x.setRowXPath("/atom:feed/atom:entry");

        x.addXPathProperty("title", "atom:title/text()");
        // x.setQueryTemplate(queryTemplate);
        RestQueryTemplate<Document> queryTemplate = new RestQueryTemplate<Document>() {

            @Override
            public WebResource constructQuery(Map<String, Object> arg,
                    WebResource wr) {
                return wr.path("feeds/issues/p").path("beacon-framework").path(
                        "issues/full");
            }


            @Override
            public Document doHandleException(Exception t) throws Exception {
                if (t instanceof WebApplicationException) {
                    WebApplicationException e = (WebApplicationException) t;
                    System.out.println(e.getResponse().getStatus());
                }
                throw t;
                // return DomUtil.newDocument();
            }


            @Override
            public Document doTransform(Object input) throws Exception {
                InputStream in = (InputStream) input;

                DocumentBuilderFactory dbf = DocumentBuilderFactory
                        .newInstance();
                dbf.setNamespaceAware(true);

                return dbf.newDocumentBuilder().parse(in);
            }

        };
        queryTemplate.setResponseEntityType(InputStream.class);
        queryTemplate.setAcceptedMediaTypes(new MediaType("application",
                "atom+xml"));
        queryTemplate.setWebResourceFactory((WebResourceFactory) VaadinContext
                .getApplicationContext()
                .getBean("googleCodeWebResourceFactory"));
        x.setQueryTemplate(queryTemplate);
        t.setContainerDataSource(x);
        t.setSizeFull();
        return t;
    }
}
